package com.starsea.book.ui.welcome

import android.app.AlertDialog
import android.content.Intent
import android.os.Bundle
import android.view.ViewGroup
import android.webkit.WebView
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.core.view.postDelayed
import androidx.core.view.setMargins
import com.blankj.utilcode.util.AppUtils
import com.blankj.utilcode.util.ConvertUtils
import com.blankj.utilcode.util.SpanUtils
import com.starsea.book.App
import com.starsea.book.R
import com.starsea.book.base.BaseActivity
import com.starsea.book.constant.PreferKey
import com.starsea.book.databinding.ActivityWelcomeBinding
import com.starsea.book.lib.theme.backgroundColor
import com.starsea.book.ui.book.read.ReadBookActivity
import com.starsea.book.ui.browser.WebViewActivity
import com.starsea.book.ui.main.MainActivity
import com.starsea.book.ui.newpage.LoginActivity
import com.starsea.book.utils.*
import com.starsea.book.utils.viewbindingdelegate.viewBinding

open class WelcomeActivity : BaseActivity<ActivityWelcomeBinding>() {

    override val binding by viewBinding(ActivityWelcomeBinding::inflate)

    override fun onActivityCreated(savedInstanceState: Bundle?) {
//        binding.ivBook.setColorFilter(accentColor)
//        binding.vwTitleLine.setBackgroundColor(accentColor)
        // 避免从桌面启动程序后，会重新实例化入口类的activity
        if (intent.flags and Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT != 0) {
            finish()
        } else {
            val key = "AGREE_TO_USE"
            val agreeToUse = getPrefBoolean(key)
            if (agreeToUse) {
                (application as? App)?.initApp()
                binding.root.postDelayed(600) { startMainActivity() }
            } else {
                val textView = TextView(this).apply {
                    SpanUtils.with(this).append(
                        "为保障您的权利，在使用我们的服务前，请通过"
                    ).append("《用户协议》").setClickSpan(
                        ContextCompat.getColor(this@WelcomeActivity, R.color.color_20002C),
                        false
                    ) {
                        startActivity<WebViewActivity> {
                            putExtra("title", "用户协议")
                            putExtra("url", "http://admin.blblcm.com/yhxy.html")
                        }
                    }.append("和").append("《隐私政策》").setClickSpan(
                        ContextCompat.getColor(this@WelcomeActivity, R.color.color_20002C),
                        false
                    ) {
                        startActivity<WebViewActivity> {
                            putExtra("title", "隐私政策")
                            putExtra("url", "http://admin.blblcm.com/ysxy.html")
                        }
                    }
                        .append("了解我们对于个人信息的使用情况与您所享有的相关权利。如您同意，请点击“同意”开始接受我们的服务。")
                        .create()
                }
                AlertDialog.Builder(this).setTitle("用户协议和隐私政策概要")
                    .setView(textView).setNegativeButton("退出") { _, _ ->
                        AppUtils.exitApp()
                    }.setPositiveButton("同意") { _, _ ->
                        (application as? App)?.initApp()
                        putPrefBoolean(key, true)
                        startMainActivity()
                    }.setCancelable(false).show()
                (textView.layoutParams as? ViewGroup.MarginLayoutParams).apply {
                    this?.setMargins(ConvertUtils.dp2px(16.0F))
                }
            }
        }
    }

    override fun setupSystemBar() {
        fullScreen()
        setStatusBarColorAuto(backgroundColor, true, fullScreen)
        upNavigationBarColor()
    }

    override fun upBackgroundImage() {
//        if (getPrefBoolean(PreferKey.customWelcome)) {
//            kotlin.runCatching {
//                when (ThemeConfig.getTheme()) {
//                    Theme.Dark -> getPrefString(PreferKey.welcomeImageDark)?.let { path ->
//                        val size = windowManager.windowSize
//                        BitmapUtils.decodeBitmap(path, size.widthPixels, size.heightPixels).let {
////                            binding.tvLegado.visible(getPrefBoolean(PreferKey.welcomeShowTextDark))
//                            binding.ivBook.visible(getPrefBoolean(PreferKey.welcomeShowIconDark))
//                            binding.tvGzh.visible(getPrefBoolean(PreferKey.welcomeShowTextDark))
//                            window.decorView.background = BitmapDrawable(resources, it)
//                            return
//                        }
//                    }
//
//                    else -> getPrefString(PreferKey.welcomeImage)?.let { path ->
//                        val size = windowManager.windowSize
//                        BitmapUtils.decodeBitmap(path, size.widthPixels, size.heightPixels).let {
//                            binding.tvLegado.visible(getPrefBoolean(PreferKey.welcomeShowText))
//                            binding.ivBook.visible(getPrefBoolean(PreferKey.welcomeShowIcon))
//                            binding.tvGzh.visible(getPrefBoolean(PreferKey.welcomeShowText))
//                            window.decorView.background = BitmapDrawable(resources, it)
//                            return
//                        }
//                    }
//                }
//            }
//        }
        super.upBackgroundImage()
    }

    private fun startMainActivity() {
        if (getPrefBoolean(PreferKey.isLogin)) {
            startActivity<MainActivity>()
            if (getPrefBoolean(PreferKey.defaultToRead)) {
                startActivity<ReadBookActivity>()
            }

        } else {
            startActivity<LoginActivity>()

        }
        finish()
    }
}

class Launcher1 : WelcomeActivity()
class Launcher2 : WelcomeActivity()
class Launcher3 : WelcomeActivity()
class Launcher4 : WelcomeActivity()
class Launcher5 : WelcomeActivity()
class Launcher6 : WelcomeActivity()